//
//  registerViewController.m
//  AdvertisementI
//
//  Created by mac on 16/5/19.
//  Copyright © 2016年 Apple inc. All rights reserved.
//

#import "registerViewController.h"
#import "loginViewController.h"
@interface registerViewController ()

@end

@implementation registerViewController
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.barTintColor = BuleColor;

}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor  = [UIColor whiteColor];
    [self registerMethod];
}
- (void)registerMethod{
    
    _registerNameView = [[VPNLabelAndTextFieldView alloc]initWithFrame:CGRectMake(15, 64, kScreenW-30, kScreenH*0.08)];
    _registerNameView.nameLabel.text = @"用户名";
    _registerNameView.valueField.placeholder = @"输入要注册的账号";
    _registerNameView.valueField.keyboardType = UIKeyboardTypeNamePhonePad;
    
    _registerCountryView = [[VPNLabelAndTextFieldView alloc]initWithFrame:CGRectMake( 15,64+kScreenH*0.08,kScreenW-30, kScreenH*0.08)];
    _registerCountryView.nameLabel.text = @"国家/地区";
    _registerCountryView.valueField.placeholder = @"";
    _registerCountryView.valueField.keyboardType = UIKeyboardTypeNamePhonePad;
    
    
    _registerPhoneNumberView= [[VPNLabelAndTextFieldView alloc]initWithFrame:CGRectMake( 15, 64+kScreenH*0.16,kScreenW-30, kScreenH*0.08)];
    _registerPhoneNumberView.nameLabel.text = @"+86";
    _registerPhoneNumberView.valueField.placeholder = @"你的手机号码";
    _registerPhoneNumberView.valueField.keyboardType = UIKeyboardTypeNumberPad;
    
    _registerPasswordView= [[VPNLabelAndTextFieldView alloc]initWithFrame:CGRectMake( 15, 64+kScreenH*0.24,kScreenW-30, kScreenH*0.08)];
    _registerPasswordView.nameLabel.text = @"密码";
    _registerPasswordView.valueField.placeholder = @"填写密码";
    _registerPasswordView.valueField.secureTextEntry = YES; //使输入框成为密码框
    
    
    _registerButton = [[UIButton alloc]initWithFrame:CGRectMake(15, 64+kScreenH*0.32+20,kScreenW-30, kScreenH*0.08)];
    [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [_registerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _registerButton.backgroundColor = [UIColor colorWithRed:100/255.0 green:199/255.0 blue:250/255.0 alpha:1];
    _registerButton.layer.borderWidth =1; //设置框线条的宽度
    _registerButton.layer.borderColor = [UIColor whiteColor].CGColor;//设置框线条的颜色
    [_registerButton.layer setCornerRadius:8];//使框的四个角为圆弧型
    [_registerButton addTarget:self action:@selector(hitRegisterBt:) forControlEvents:UIControlEventTouchUpInside];
    _imgview = [[UIImageView alloc]initWithFrame:CGRectMake(15, 64+kScreenH*0.4+20, kScreenH-400, kScreenH*0.4)];
    //    _imgview.image = [UIImage imageNamed:@"3.jpg"];
    [self.view addSubview:_imgview];
    
    
    [self.view addSubview:_registerNameView];
    [self.view addSubview:_registerCountryView];
    [self.view addSubview:_registerPhoneNumberView];
    [self.view addSubview:_registerPasswordView];
    [self.view addSubview:_registerButton];
    
    
}
//点击注册界面的注册按钮
- (void)hitRegisterBt:(UIButton *)sender{
    [self.view endEditing:YES];
    if ([self validateNickname:_registerNameView.valueField.text]) {
        if ([self isMobile:_registerPhoneNumberView.valueField.text]) {
            if ([self isPassword:_registerPasswordView.valueField.text]) {
    
             AFHTTPRequestOperationManager *manager =[[AFHTTPRequestOperationManager alloc]init];
                manager.responseSerializer =[AFHTTPResponseSerializer serializer];
                NSDictionary *parameters = @{@"name":_registerNameView.valueField.text, @"password":_registerPasswordView.valueField.text,@"tel":_registerPhoneNumberView.valueField.text};
                [manager POST:@"http://47.88.76.185:8080/Test/app_addUser1" parameters:parameters//19.232
                      success:^(AFHTTPRequestOperation *operation, id responseObject)
                 {
                     NSDictionary*dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
                     NSString *rcode = [dic objectForKey:@"rcode"];
                     int a = [rcode intValue];
                     NSLog(@"----%@",parameters);
                     if (!a)
                     {
                         //                         [self.window showHUDWithText:rcode Type:ShowPhotoNo Enabled:YES];
                         AlertController(@"用户名已注册过")
                     }
                     else
                     {  [self.navigationController popViewControllerAnimated:YES];
                         AlertController(@"注册成功")

                         
                     }
                 } failure:nil];
            }else{
                AlertController(@"密码为6到12位的字母或者数字")
            }
        }else{
            AlertController(@"手机号码不存在")
        }
    }else{
        AlertController(@"名字为2到10个中文,字母或者数字")
    }
}
//手机号码的正则表达式
- (BOOL) isMobile:(NSString *)mobileNumbel{
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189,181(增加)
     */
    NSString * MOBIL = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[2378])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189,181(增加)
     22         */
    NSString * CT = @"^1((33|53|8[019])[0-9]|349)\\d{7}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBIL];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNumbel]
         || [regextestcm evaluateWithObject:mobileNumbel]
         || [regextestct evaluateWithObject:mobileNumbel]
         || [regextestcu evaluateWithObject:mobileNumbel])) {
        return YES;
    }
    
    return NO;
}
//判断密码是否为6到12位的字母或者数字
- (BOOL)isPassword:(NSString *)password
{
    NSString *      regex = @"(^[A-Za-z0-9]{6,20}$)";
    NSPredicate *   pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    return [pred evaluateWithObject:password];
}
//判断名字是否为2到10个字节的中文或者汉字
- (BOOL) validateNickname:(NSString *)nickname
{
    NSString *nicknameRegex = @"[\u4e00-\u9fa5_a-zA-Z0-9_]{2,10}";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
    return [passWordPredicate evaluateWithObject:nickname];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
